import {Body, Controller, Ctx, Delete, Flow, Get, Params, Post, Put} from 'koa-ts-controllers'
import {Context} from "koa";
import authorization from "../middlewares/authorization";
import {
    getandValidateBoard, getCA,
    getcards,
    getcomment,
    getlist,
    PostAddBoadtBody,
    PutUpdateBoard
} from "../validators/Board";
import {Board as BoardModel} from "../models/Board";
import {BoardList as BoardListModel} from "../models/BoardList";
import {BoardListCard as BoardListCardModel} from "../models/BoardListCard";
import {CardAttachment as CardAttachmentModel} from "../models/CardAttachment";
import {Comment as CommentModel} from "../models/Comment";
import {Attachment as AttachmentModel} from "../models/Attachment";
import delfile from "../attachments/delfile";
@Controller('/board')
export class BoradController {
    @Post('')
    @Flow([authorization])
    public async addBoard(@Ctx() ctx: Context, @Body() body: PostAddBoadtBody) {
        let {name} = body;
        let board = new BoardModel;
        board.name = name;
        board.userId = ctx.userInfo.id;
        await board.save();
        ctx.status = 201;
        return board;

    }

    @Get('')
    // @Flow([authorization])
    public async getBoars(@Ctx() ctx: Context) {
        let where = {
            userId: ctx.userInfo.id
        };
        let boards = await BoardModel.findAll({where})
        return boards;
    }

    @Get('/:id(\\d+)')
    public async getBoard(@Ctx() ctx: Context, @Params('id') id: number) {
        console.log(ctx.userInfo.id)
        console.log(id)
        let board = await getandValidateBoard(id, ctx.userInfo.id);
        return board
    }

    @Put('/:id(\\d+)')
    public async updateBoadt(@Ctx() ctx: Context, @Params('id')id: number, @Body() body: PutUpdateBoard) {
        let board = await getandValidateBoard(id, ctx.userInfo.id);
        board.name = body.name || board.name;
        await board.save();
        ctx.status = 204
    }

    @Delete('/:id(\\d+)')
    public async deleteBoard(@Ctx() ctx: Context, @Params('id') id: number) {
        let board = await getandValidateBoard(id, ctx.userInfo.id);
        let lists = await getlist(id, ctx.userInfo.id)
        let setlists: any[] = []
        let setcard: any[] = []
        let setCom: any[] = []
        let setCA: any[] = []
        let setAttachment: any[] = []
        if(lists.length!=0){
            for (let a of lists) {
                setlists.push(a.id)
                console.log('查询list')
            }
            let cards = await getcards(setlists)
            if(cards.length!=0){
                for (let a of cards) {
                    console.log(JSON.stringify(a))
                    let card = await BoardListCardModel.findOne({
                        where: {
                            id: a.id
                        }
                    })
                    // @ts-ignore
                    setcard.push(card.id)
                    console.log('查询card')
                }
                console.log('getcard'+JSON.stringify(setcard))
                if(setcard.length!=0){
                    //com
                    setCom = await getcomment(setcard)
                    if(setCom.length!=0){
                        for(let a of setCom){
                            let comment = await CommentModel.findOne({
                                where: {
                                    id: a.id
                                }
                            })
                            console.log('删除com')
                            // @ts-ignore
                            await comment.destroy()
                        }
                        console.log('setcom'+setCom)
                    }
// ca
                    setCA = await getCA(setcard)
                    if(setCA.length!=0){

                        for (let a of setCA) {
                            let ca = await CardAttachmentModel.findOne({
                                where: {
                                    id: a.id
                                }
                            })
                            console.log('删除ca')
                            // @ts-ignore
                            await ca.destroy()
                        }
                        for (let a of setCA) {
                            let attachment = await AttachmentModel.findOne({
                                where: {
                                    id: a.id
                                }
                            })
                            // @ts-ignore
                            setAttachment.push(attachment.name)
                            console.log('删除attachment')
                            // @ts-ignore
                            await attachment.destroy()
                        }
                        console.log('file'+setAttachment)
                        delfile(setAttachment)
                    }
                }
                for (let a of cards) {
                    console.log(JSON.stringify(a))
                    let card = await BoardListCardModel.findOne({
                        where: {
                            id: a.id
                        }
                    })
                    console.log('删除card')
                    // @ts-ignore
                    await card.destroy()
                }
            }
            for (let a of lists) {
                let list = await BoardListModel.findOne({
                    where: {
                        boardId: board.id
                    }
                })
                console.log('删除list')
                // @ts-ignore
                await list.destroy()
            }
        }
        console.log('删除board')
        await board.destroy();
        ctx.status = 204
    }
}




